package kit.edu.lego.gruppe3;

import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.NXTRegulatedMotor;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
import lejos.nxt.UltrasonicSensor;
import lejos.robotics.localization.OdometryPoseProvider;
import lejos.robotics.localization.PoseProvider;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.navigation.Move;
import lejos.robotics.navigation.MoveListener;
import lejos.robotics.navigation.MoveProvider;
import lejos.robotics.navigation.NavPathController;

public class ComponentsRepository {
	
	private UltrasonicSensor sonicSensor = new UltrasonicSensor(SensorPort.S1);
	private LightSensor lightSensor = new LightSensor(SensorPort.S3);
	private TouchSensor rightTouchSensor = new TouchSensor(SensorPort.S4);
	private TouchSensor leftTouchSensor = new TouchSensor(SensorPort.S2);
	private NXTRegulatedMotor rightWheel = Motor.A;
	private NXTRegulatedMotor middleMotor = Motor.B;
	private NXTRegulatedMotor leftWheel = Motor.C;
	
	private DifferentialPilot robotPilot = 
		new DifferentialPilot(1.1f, 6.1f, leftWheel, rightWheel);
	private MoveProvider moveProvider = new MoveProvider(){
		
		@Override
		public void addMoveListener(MoveListener paramMoveListener) {
			robotPilot.addMoveListener(paramMoveListener);
		}

		@Override
		public Move getMovement() {
			return robotPilot.getMovement();
		}
		
	};
	private PoseProvider poseProvider = new OdometryPoseProvider(moveProvider);
	private NavPathController controller = new NavPathController(robotPilot,poseProvider);

	public static final int LIGHT_TRASHOLD = 35;
	public static final int LIGHT_MAX_TRASHOLD = 49;
	public static final int DISTANCE_TRASHOLD = 10;
	public static final int BRIDGE_LIGHT_TRASHOLD = 31;
	
	public static final int INIT_SENSORS_ANGLE = 90;
	public static final double VEHICLE_LENGTH = 8;
	
	public static boolean endOfLine = false;
	public static boolean evadeObstacle = false;
	/**
	 * Gives the info if the robot has reached the line
	 * @param lightValue light sensor input
	 * @return
	 */
	public boolean isOnLine(int lightValue){
		return (lightValue > LIGHT_TRASHOLD) && (lightValue < LIGHT_MAX_TRASHOLD);
	}
	
	/**
	 * Gives the info if the robot is on black ground
	 * @param lightValue light sensor input
	 * @return
	 */
	public boolean isOnBlackGround(int lightValue){
		return (lightValue < LIGHT_TRASHOLD);
	}
	
	
	private static ComponentsRepository instance = null;
	
	private static char lastTurn = 'r';
	
	private ComponentsRepository(){
	
		
	}
	
	public static ComponentsRepository getInstance(){
		if (instance != null){
			return instance;
		} else {
			instance = new ComponentsRepository();
			return instance;
		}
	}

	public UltrasonicSensor getSonicSensor() {
		return sonicSensor;
	}

	public LightSensor getLightSensor() {
		return lightSensor;
	}

	public TouchSensor getLeftTouchSensor() {
		return leftTouchSensor;
	}

	public NXTRegulatedMotor getRightWheel() {
		return rightWheel;
	}

	public NXTRegulatedMotor getLeftWheel() {
		return leftWheel;
	}

	public NXTRegulatedMotor getMiddleMotor() {
		return middleMotor;
	}
	
	public DifferentialPilot getRobotPilot() {
		return robotPilot;
	}

	public TouchSensor getRightTouchSensor() {
		return rightTouchSensor;
	}

	public static char getLastTurn()
	{
		return lastTurn;
	}
	public static void setLastTurn(char lT)
	{
		lastTurn = lT;
	}
	
	public NavPathController getController() {
		return controller;
	}
}
